package com.example.springboot.controller;

import com.example.springboot.common.Result;

import com.example.springboot.entity.jobs;
import com.example.springboot.service.JobsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/jobs")
public class JobsController {

    @Autowired
    private JobsService jobsService;

    @GetMapping
    public Result getAllJobs() {
        try {
            List<jobs> jobs = jobsService.findAllJobs();
            return Result.success(jobs);
        } catch (Exception e) {
            return Result.error("500", "获取职位列表失败");
        }
    }

    @GetMapping("/{id}")
    public Result getJobById(@PathVariable int id) {
        try {
            jobs job = jobsService.findJobById(id);
            return Result.success(job);
        } catch (Exception e) {
            return Result.error("500", "获取职位详情失败");
        }
    }

    @PostMapping
    public Result createJob(@RequestBody jobs job) {
        try {
            jobsService.saveJob(job);
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", "创建职位失败");
        }
    }

    @PutMapping("/{id}")
    public Result updateJob(@PathVariable int id, @RequestBody jobs job) {
        try {
            job.setJob_id(id);
            jobsService.saveJob(job);
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", "更新职位失败");
        }
    }


    @DeleteMapping("/{id}")
    public Result deleteJob(@PathVariable int id) {
        try {
            jobsService.deleteJob(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", "删除职位失败");
        }
    }
}
